﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using System.Text;
using System.Linq.Expressions;
using Services;

namespace WebUI.Extensions
{
    public static class HtmlHelperExtensions
    {
        public static IHtmlString ToolTip(this HtmlHelper helper, String input, int length)
        {
            if (string.IsNullOrEmpty(input) || input.Length <= length)
            {
                return new MvcHtmlString(input);
            }
            else
            {
                var split = input.Split(new char[1] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                var token = split[split.Count() - 1];
                return new MvcHtmlString(String.Format("<span title=\"{0}\">...{1}</span>", input, token));
            }
        }

        public static IHtmlString EnumText<T>(this HtmlHelper helper, int value)
        {
            var enumName = Enum.ToObject(typeof(T), value).ToString();
            return new MvcHtmlString(enumName);
        }

        public static IHtmlString BooleanToText(this HtmlHelper helper, bool value)
        {
            string text = value ? "yes" : "no";
            return new MvcHtmlString(text);
        }

        public static IHtmlString ArrayToTextFor<TModel, TKey>(this HtmlHelper<TModel> helper, Expression<Func<TModel, IEnumerable<TKey>>> expression, object htmlAttributes = null)
        {
            MemberExpression body = expression.Body as MemberExpression;
            string propertyName = body.Member.Name;

            IEnumerable<TKey> list = expression.Compile().Invoke(helper.ViewData.Model);
            ICSVService cSVService = new CSVService();
            string text = cSVService.ConstructDelimitedString(list);

            //Create div
            var textBox = new TagBuilder("input");
            textBox.Attributes.Add(new KeyValuePair<string, string>("type", "text"));
            textBox.Attributes.Add(new KeyValuePair<string, string>("name", propertyName));
            textBox.Attributes.Add(new KeyValuePair<string, string>("id", propertyName));
            if (text.Length != 0)
            {
                textBox.Attributes.Add(new KeyValuePair<string, string>("value", text));
            }
            textBox.MergeAttributes(new RouteValueDictionary(htmlAttributes), true);

            return MvcHtmlString.Create(textBox.ToString());
        }

        public static IHtmlString ConstructDelimitedString(this HtmlHelper helper, IEnumerable<string> list)
        {
            ICSVService csvService = new CSVService();
            var result = csvService.ConstructDelimitedString(list);

            return new MvcHtmlString(result);
        }

        public static IHtmlString Popup(this HtmlHelper helper, string field, string actionName, int rowId, char identifier)
        {
            string url = "";
            UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

            if (actionName == MVC.Reports.Summary.Actions.ActionNames.DirectoryOwner)
            {
                url = urlHelper.Action(MVC.Reports.Summary.DirectoryOwner(field));
            }
            else if (actionName == MVC.Reports.Summary.Actions.ActionNames.FileSummary)
            {
                url = urlHelper.Action(MVC.Reports.Summary.FileSummary(field));
            }
            else if (actionName == MVC.Reports.Summary.Actions.ActionNames.MachineTopAccessors)
            {
                url = urlHelper.Action(MVC.Reports.Summary.MachineTopAccessors(field));
            }
            else if (actionName == MVC.Reports.Summary.Actions.ActionNames.UserSummary)
            {
                url = urlHelper.Action(MVC.Reports.Summary.UserSummary(field));
            }


            var result = string.Format(Resources.Popup, field, url, rowId, identifier);
            return new MvcHtmlString(result);
        }
    }
}